package com.primecodegen.common.bean;

/**
 *
 * @author Mohammad
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.primecodegen.common.DataModelObject.DataModelObjectDAO;
import com.primecodegen.common.DataModelObject.DataModelObjectTable;
import com.primecodegen.common.GeneratedTargets.GeneratedTargetsDAO;
import com.primecodegen.common.GeneratedTargets.GeneratedTargetsTable;
import com.primecodegen.common.base.BaseBean;
import com.primecodegen.core.processTemplate_1;
import com.primecodegen.util.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "step6Bean")
@SessionScoped
public class Step6Bean extends BaseBean implements Serializable {

    private List<GeneratedTargetsTable> generatedTargetsTables;
    private GeneratedTargetsTable[] selectedGeneratedTargetsTables;
    private DataModelObjectTable[] selectedObjectsTables;
    private GeneratedTargetsTable generatedTargetsTable = new GeneratedTargetsTable();
    private GeneratedTargetsDAO generatedTargetsDAO = new GeneratedTargetsDAO();
    private DataModelObjectDAO dataModelObjectDAO = new DataModelObjectDAO();
    FacesContext context = FacesContext.getCurrentInstance();
    processTemplate_1 template = new processTemplate_1();
    ArrayList<DataModelObjectTable> dataModelObjectTables = new ArrayList<DataModelObjectTable>();
    GenerateWizardBean g = (GenerateWizardBean) context.getExternalContext().getSessionMap().get("generateWizardBean");
    String generateToRoot = "C:\\Users\\Mohammad\\Documents\\NetBeansProjects\\";

    public Step6Bean() {
        generatedTargetsTables = new ArrayList<GeneratedTargetsTable>();
        fillGeneratedTargets();
    }

    public void fillGeneratedTargets() {
        try {
            GeneratedTargetsTable criteria = new GeneratedTargetsTable();
            criteria.setMainProject_ID(g.getSelectedMainProjectTable().getID());
            generatedTargetsTables = (ArrayList<GeneratedTargetsTable>) generatedTargetsDAO.getByCriterionOrderedBy(criteria, null);
        } catch (Exception ex) {
            Logger.getLogger(GenerateWizardBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addNewGeneratedTargets() {
        try {
            int insert = generatedTargetsDAO.insert(new GeneratedTargetsTable(g.getSelectedMainProjectTable().getID()));
        } catch (Exception ex) {
            Logger.getLogger(Step6Bean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void getAllDataModelObjects() {
        dataModelObjectTables = new ArrayList<DataModelObjectTable>();
        DataModelObjectTable groupTable = new DataModelObjectTable();
        DataModelObjectTable srchCriteria = new DataModelObjectTable();
        srchCriteria.setDataModelProject_ID(g.getSelectedDataModelProjectTable().getID());
        if (!util.isEmpty(g.getSelectedDataModelProjectTable().getID())) {
            srchCriteria.setFilter(true);
        }
        try {

            this.dataModelObjectTables = (ArrayList<DataModelObjectTable>) dataModelObjectDAO.getByCriterionOrderedBy(srchCriteria, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateGeneratedTarget() {
        try {
            int updateByID = -1;
            updateByID = generatedTargetsDAO.updateByID(generatedTargetsTable);
            if (updateByID != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Update completed", ""));
                fillGeneratedTargets();
            }
        } catch (Exception ex) {
            getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Exception", "exception :" + ex));
            Logger.getLogger(GenerateWizardBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    /**
     * 
     */
    public void doTempleting() {
        try {
            template.doProcessing(g.getSelectedDataModelProjectTable(), getSelectedObjectsTables(), getSelectedGeneratedTargetsTables(), generateToRoot);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doAddNewGenTarget() {
        try {
          //  g = (GenerateWizardBean) context.getExternalContext().getSessionMap().get("generateWizardBean");
            System.out.println("g.getSelectedMainProjectTable().getID() : " + g.getSelectedMainProjectTable().getID());
            int insert = generatedTargetsDAO.insert(new GeneratedTargetsTable("","","","",g.getSelectedMainProjectTable().getID()));
            fillGeneratedTargets();
        } catch (Exception ex) {
            Logger.getLogger(Step6Bean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //<editor-fold defaultstate="collapsed" desc="Getters and Setetrs">
    public GeneratedTargetsTable getGeneratedTargetsTable() {
        return generatedTargetsTable;
    }

    public void setGeneratedTargetsTable(GeneratedTargetsTable generatedTargetsTable) {
        this.generatedTargetsTable = generatedTargetsTable;
    }

    public List<GeneratedTargetsTable> getGeneratedTargetsTables() {
        return generatedTargetsTables;
    }

    public void setGeneratedTargetsTables(List<GeneratedTargetsTable> generatedTargetsTables) {
        this.generatedTargetsTables = generatedTargetsTables;
    }

    public GeneratedTargetsTable[] getSelectedGeneratedTargetsTables() {
        return selectedGeneratedTargetsTables;
    }

    public void setSelectedGeneratedTargetsTables(GeneratedTargetsTable[] selectedGeneratedTargetsTables) {
        this.selectedGeneratedTargetsTables = selectedGeneratedTargetsTables;
    }

    public DataModelObjectTable[] getSelectedObjectsTables() {
        return selectedObjectsTables;
    }

    public void setSelectedObjectsTables(DataModelObjectTable[] selectedObjectsTables) {
        this.selectedObjectsTables = selectedObjectsTables;
    }

    public ArrayList<DataModelObjectTable> getDataModelObjectTables() {
        getAllDataModelObjects();
        return dataModelObjectTables;
    }

    public void setDataModelObjectTables(ArrayList<DataModelObjectTable> dataModelObjectTables) {
        this.dataModelObjectTables = dataModelObjectTables;
    }

    public String getGenerateToRoot() {
        return generateToRoot;
    }

    public void setGenerateToRoot(String generateToRoot) {
        this.generateToRoot = generateToRoot;
    }
    //</editor-fold>
}
